package com.wxq.draw;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wxq.shape.MapCircle;
import com.wxq.shape.MapPath;
import com.wxq.shape.MapShape;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.View;

public class DrawrouteVeiw extends View {
	protected Paint defaultStrokePaint;
	protected float xOffset = 0.0f;
	protected float yOffset = 0.0f;
	protected Matrix scaleMatrix = new Matrix();
	protected MapPath mapPath;
	protected boolean disapear=true;//缩放游标
	protected float scale = 1.5f;//缩放比例 初始
	protected String nowfloor=null;
	public List<Map<String,Object>> piontlist=new ArrayList<Map<String,Object>>();
	
	
	//缩放比例范围
	{		
		defaultStrokePaint = new Paint();
		defaultStrokePaint.setAntiAlias(true);    //设置无锯齿
		defaultStrokePaint.setStyle(Style.STROKE);  //设置填充方式
		defaultStrokePaint.setColor(Color.RED);
		defaultStrokePaint.setStrokeWidth(10.0f);//线宽
	}

	public DrawrouteVeiw(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		 if(disapear!=true){
		  
			Path path = new Path();
		    scaleMatrix.setScale(this.scale, this.scale);
		   Boolean j=false;
           if(!piontlist.isEmpty()){
              for(int i=0;i<piontlist.size();i++){
            	 if(piontlist.get(i).get("FLOOR").toString().equals(nowfloor)){
	                float xx=Float.parseFloat(piontlist.get(i).get("Coord_X").toString());
	        		float yy=Float.parseFloat(piontlist.get(i).get("Coord_Y").toString());
	        		   Log.d("route", xx+yy+piontlist.get(i).get("FLOOR").toString()+nowfloor);
	        		MapShape ms = new MapCircle(xx,yy,1);
	         		ms.offset(xOffset, yOffset);
	                ms.zoom(scaleMatrix);
	        		Paint iPaint= defaultStrokePaint;
	        		ms.draw(canvas, iPaint);
	        		if (j==false) {
	        			path.moveTo(xx, yy);
	        			j=true;
	    			}else {
	    				path.lineTo(xx, yy);
	    			}
           }}}
           mapPath = new MapPath(path);
           mapPath.offset(xOffset, yOffset);
           mapPath.zoom(scaleMatrix);
           mapPath.draw(canvas, defaultStrokePaint);
     	}
	}
	//----------------缩放相关，每个图元都自己缩放一次-------------------------------------------------
	void zoom(float scale) {
		this.scale = scale;
		scaleMatrix = new Matrix();
		scaleMatrix.setScale(this.scale, this.scale);
		invalidate();
	}
	
	//----------------位移相关-------------------------------------------------
	public void scrollByDistance(float distanceX, float distanceY) {
		xOffset = (float) (xOffset-distanceX);
		yOffset = (float) (yOffset-distanceY);
		invalidate();
	}
	public void scrollBypoint(float distanceX, float distanceY){
	xOffset = (float) distanceX;
	yOffset = (float) distanceY;
     }
	public void DrawRoute(List<Map<String,Object>> piont,String floor){		
		piontlist=new ArrayList<Map<String,Object>>();
	    piontlist.addAll(piont);
		nowfloor=floor;
		disapear=false;
		invalidate();
	}
	public void changefloor(String floor){
		nowfloor=floor;
		disapear=false;
		invalidate();
	}
	
	public void hideRoute(){
		disapear=true;
		invalidate();
	}
}
